#!/bin/sh -x

# Oracle/prefix paths
PREFIX="${PKG_PREFIX}"
ORACLEPREFIX="/usr/lib/oracle"

# default Oracle user/group/directory, should be propagated from the Makefile
ORAUSER=${ORACLEUSER:-oracle}
ORAGROUP=${ORACLEGROUP:-dba}
ORADIR="${ORACLEPREFIX}/xe"

case $2 in
PRE-INSTALL)
#
# preinstall script extracted from the OracleXE RPM package
# and adapted for FreeBSD environment
#

# User must be root

if [ $(id -u) != "0" ]
then
    echo "You must be the root to install the software" >&2
    exit 1
fi

# Check for sufficient diskspace
if [ -d "$ORACLEPREFIX" ] 
then
	target="$ORACLEPREFIX"
else
	target=`dirname "$ORACLEPREFIX"`
fi
diskspace=`df -k "$target" | grep % | tr -s " " | cut -d" " -f4 | tail -1`
diskspace=`expr $diskspace / 1024`
if [ $diskspace -lt 100 ]
then
	echo "You have insufficient diskspace in the destination directory ($ORACLEPREFIX) 
to install Oracle Client 10g Express Edition.  The installation requires at 
least 100 MB free on this disk."
        exit 1
fi

# End of pre-install script
exit 0
;; 


POST-INSTALL)
#
# postinstall script extracted from the OracleXE RPM package
# and adapted for FreeBSD environment
#
echo "Executing Post-install steps..."

/usr/sbin/chown -R $ORAUSER:$ORAGROUP $ORADIR
#XXX
/compat/linux/sbin/ldconfig >/dev/null

chmod 755 $ORADIR/app/oracle/product/10.2.0/client/bin/sqlplus

# Start Menu icons

if test -d  /var/lib/menu/kde/Applications/oraclexe-client
then
	echo ""
else
	mkdir -p /var/lib/menu/kde/Applications/oraclexe-client/gethelp
fi

if [ -f /var/lib/menu/kde/Applications/oraclexe-client/.directory ]
then
	rm -fr /var/lib/menu/kde/Applications/oraclexe-client/.directory
	ln -s /usr/share/desktop-menu-files/oraclexe-client-10g.directory /var/lib/menu/kde/Applications/oraclexe-client/.directory
else
	ln -s /usr/share/desktop-menu-files/oraclexe-client-10g.directory /var/lib/menu/kde/Applications/oraclexe-client/.directory
fi

if [ -f /var/lib/menu/kde/Applications/oraclexe-client/oraclexe-client-runsql.desktop ]
then
	rm -fr /var/lib/menu/kde/Applications/oraclexe-client/oraclexe-client-runsql.desktop
	ln -s /usr/share/applications/oraclexe-client-runsql.desktop /var/lib/menu/kde/Applications/oraclexe-client/oraclexe-client-runsql.desktop
else
	ln -s /usr/share/applications/oraclexe-client-runsql.desktop /var/lib/menu/kde/Applications/oraclexe-client/oraclexe-client-runsql.desktop
fi

if [ -f /var/lib/menu/kde/Applications/oraclexe-client/gethelp/.directory ] 
then
       rm -fr /var/lib/menu/kde/Applications/oraclexe-client/gethelp/.directory
       ln -s /usr/share/desktop-menu-files/oraclexe-client-gethelp.directory /var/lib/menu/kde/Applications/oraclexe-client/gethelp/.directory
else
	ln -s /usr/share/desktop-menu-files/oraclexe-client-gethelp.directory /var/lib/menu/kde/Applications/oraclexe-client/gethelp/.directory
fi

if [ -f /var/lib/menu/kde/Applications/oraclexe-client/gethelp/oraclexe-client-registerforonlineforum.desktop ]
then
	rm -fr /var/lib/menu/kde/Applications/oraclexe-client/gethelp/oraclexe-client-registerforonlineforum.desktop
	ln -s /usr/share/applications/oraclexe-client-registerforonlineforum.desktop /var/lib/menu/kde/Applications/oraclexe-client/gethelp/oraclexe-client-registerforonlineforum.desktop
else
	ln -s /usr/share/applications/oraclexe-client-registerforonlineforum.desktop /var/lib/menu/kde/Applications/oraclexe-client/gethelp/oraclexe-client-registerforonlineforum.desktop
fi

if [ -f /var/lib/menu/kde/Applications/oraclexe-client/gethelp/oraclexe-client-readdocumentation.desktop ]
then
	rm -fr  /var/lib/menu/kde/Applications/oraclexe-client/gethelp/oraclexe-client-readdocumentation.desktop
	ln -s /usr/share/applications/oraclexe-client-readdocumentation.desktop /var/lib/menu/kde/Applications/oraclexe-client/gethelp/oraclexe-client-readdocumentation.desktop
else
	ln -s /usr/share/applications/oraclexe-client-readdocumentation.desktop /var/lib/menu/kde/Applications/oraclexe-client/gethelp/oraclexe-client-readdocumentation.desktop
fi

if [ -f /var/lib/menu/kde/Applications/oraclexe-client/gethelp/oraclexe-client-gotoonlineforum.desktop ]
then
	rm -fr /var/lib/menu/kde/Applications/oraclexe-client/gethelp/oraclexe-client-gotoonlineforum.desktop
	ln -s /usr/share/applications/oraclexe-client-gotoonlineforum.desktop /var/lib/menu/kde/Applications/oraclexe-client/gethelp/oraclexe-client-gotoonlineforum.desktop
else
	ln -s /usr/share/applications/oraclexe-client-gotoonlineforum.desktop /var/lib/menu/kde/Applications/oraclexe-client/gethelp/oraclexe-client-gotoonlineforum.desktop
fi

if [ -f /etc/xdg/menus/applications.menu ]
then
	cp -f /usr/share/desktop-menu-files/oraclexe-client-10g.directory /usr/share/desktop-directories/oraclexe-client-10g.directory
	cp -f /usr/share/desktop-menu-files/oraclexe-client-gethelp.directory /usr/share/desktop-directories/oraclexe-client-gethelp.directory
	cp -f /usr/lib/oracle/xe/app/oracle/product/10.2.0/client/scripts/oraclexe-client.menu /etc/xdg/menus
	sed -i '1,/<\/Menu>/ { /<\/Menu>/ r /usr/lib/oracle/xe/app/oracle/product/10.2.0/client/scripts/oraclexe-client-merge.menu 
	}' /etc/xdg/menus/applications.menu 

elif [ -f /etc/X11/desktop-menus/applications.menu ]
then
#	sed -i '1,/<\/Query>/ { /<\/Query>/ r /usr/lib/oracle/xe/app/oracle/product/10.2.0/client/scripts/oraclexe-client-rhel3.menu 
	sed -i '1,/<\/Query>/ { /<\/Query>/ r /usr/lib/oracle/xe/app/oracle/product/10.2.0/client/scripts/oraclexe-client-rhel3.menu
	}' /etc/X11/desktop-menus/applications.menu  
elif [ -f /etc/xdg/menus/applications-merged/kde-essential.menu ]
then
        cp -f /usr/share/desktop-menu-files/oraclexe-client-10g.directory /usr/share/desktop-directories/oraclexe-client-10g.directory
        cp -f /usr/share/desktop-menu-files/oraclexe-client-gethelp.directory /usr/share/desktop-directories/oraclexe-client-gethelp.directory
        cp -f /usr/lib/oracle/xe/app/oracle/product/10.2.0/client/scripts/oraclexe-client.menu /etc/xdg/menus
        sed -i '1,/<\/Menu>/ { /<\/Menu>/ r /usr/lib/oracle/xe/app/oracle/product/10.2.0/client/scripts/oraclexe-client-asianux.menu
        }' /etc/xdg/menus/applications-merged/kde-essential.menu
fi

rm -fr $ORACLEPREFIX/screenrc
rm -fr $ORACLEPREFIX/gtkrc
rm -fr $ORACLEPREFIX/emacs
rm -fr $ORACLEPREFIX/cshrc-DEFAULT_old
rm -fr $ORACLEPREFIX/cshrc-DEFAULT.06292004
rm -fr $ORACLEPREFIX/cshrc-DEFAULT
rm -fr $ORACLEPREFIX/cshrc
rm -fr $ORACLEPREFIX/bashrc-DEFAULT
rm -fr $ORACLEPREFIX/bashrc_logout

# End of post-install script
exit 0
;;

*)
  ;;

esac
